package com.tangr.adapterapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.tangr.normaladapter.ViewHolder;
import com.tangr.normaladapter.recyclerview.MultiItemAdapter;
import com.tangr.normaladapter.recyclerview.MultiItemTypeSupport;
import com.tangr.normaladapter.recyclerview.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

public class LXRActivity extends AppCompatActivity {
    private ContactRecyclerView mRecyclerView;
    private List<Contact> mDatas = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lxr);
        initDatas();

        mRecyclerView = (ContactRecyclerView) findViewById(R.id.id_recyclerview);

        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

        final MultiItemAdapter<Contact> adapter = new MultiItemAdapter<Contact>(this, mDatas, new MultiItemTypeSupport<Contact>() {
            @Override
            public int getLayoutId(int type) {
                if (type==0)
                    return R.layout.string_item;
                return R.layout.list_item;
            }

            @Override
            public int getItemViewType(int position, Contact contact) {
                if (contact.isTitle)
                    return 0;
                return 1;
            }

            @Override
            public boolean isEnabled(int viewType){
                if(viewType==0)
                    return false;
                return true;
            }
        }) {
            @Override
            public void setView(ViewHolder holder, Contact item) {
                if(holder.getLayoutId()==R.layout.list_item){
                    holder.setText(R.id.item_tv,item.name);
                    holder.setImage(R.id.item_iv, R.mipmap.ic_launcher);
                }else {
                    holder.setText(R.id.item_tv, item.name);
                    holder.itemView.setTag(R.id.tag_itemView,1);
                }
                holder.itemView.setContentDescription(item.cate);
            }
        };
        adapter.setOnItemClickListener(new OnItemClickListener<Contact>() {
            @Override
            public void onItemClick(ViewGroup parent, View view, Contact s, int position) {
                Toast.makeText(LXRActivity.this, "click" + position + ":" + s.name, Toast.LENGTH_SHORT).show();
            }

            @Override
            public boolean onItemLongClick(ViewGroup parent, View view, Contact s, int position) {
                Toast.makeText(LXRActivity.this, "longclick" + position + ":" + s.name, Toast.LENGTH_SHORT).show();
                return true;
            }
        });
//        adapter.setHeader(R.layout.sample_header);
        mRecyclerView.setAdapter(adapter);
        mRecyclerView.setHeaderLayout(R.layout.string_item);
    }

    private void initDatas()
    {
        for (int i = 'A'; i < 'H'; i++)
        {
            mDatas.add(new Contact((char) i + "",true,(char) i + ""));
            for (int j=0;j<new Random().nextInt(10)+1;j++){
                mDatas.add(new Contact((char) i + "" + j,false,(char) i + ""));
            }
        }
    }
}
